import SparkMD5 from "spark-md5"
console.log(SparkMD5);
window.addEventListener('load', function () {
    let file = this.document.getElementById('file')
    let chunks = 0
    file.onchange = function (e) {
        startChunk(e)
    }
    function startChunk(e) {
        let currentChunk = 0
        let chunkSize = 50 * 1024 * 1024
        var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
        let file = e.target.files[0]
        chunks = Math.ceil(file.size / chunkSize)
        console.log(chunks, 'chunks');
        let spark = new SparkMD5.ArrayBuffer()
        let result = []
        let fileReader = new FileReader();
        let time = new Date().getTime()
        fileReader.onload = function (e) {
            spark.append(e.target.result);                   // Append array buffer
            // Compute hash
            result.push({
                start: currentChunk,
                hash: spark.end()
            })
            currentChunk++;
            loadNext()
        };
        fileReader.onerror = function () {
            console.warn('oops, something went wrong.');
        };

        function loadNext() {
            console.log(currentChunk, chunks);
            if (currentChunk < chunks) {
                var start = currentChunk * chunkSize,
                    endIndex = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
                fileReader.readAsArrayBuffer(blobSlice.call(file, start, endIndex));
            } else {
                console.log(result);
            }
        }

        loadNext()
    }

})